using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Windows.Forms;


namespace ClientGUI
{
    class DisplayChildTags:Control
    {
        ListView my_view;
        Module my_current;
        Button my_addbutton;
        DisplayDocument my_host;

        public DisplayChildTags(DisplayDocument host)
        {
            my_host = host;

            my_view = new ListView();
            InitializeList();

            my_addbutton = new Button();
            my_addbutton.Text = "<< Add";
            my_addbutton.Click += new EventHandler(my_addbutton_Click);
            my_addbutton.FlatStyle = FlatStyle.Flat;
            my_addbutton.AutoSize = true;
            Controls.Add(my_view);
            Controls.Add(my_addbutton);

            my_addbutton.Show();

            my_view.MultiSelect = false;
            my_view.FullRowSelect = true;
            my_view.View = View.Details;
            my_view.ShowGroups = false;
            my_view.MultiSelect = false;
            my_view.HideSelection = false;
            my_view.HeaderStyle = ColumnHeaderStyle.Nonclickable;
            my_view.GridLines = true;
            my_view.LabelEdit = false;

            my_view.Show();

            Resize += new EventHandler(DisplayChildTags_Resize);
        }

        public ListView ListView { get { return my_view; } }

        void DisplayChildTags_Resize(object sender, EventArgs e)
        {
            //my_addbutton.Height = 20;
            my_addbutton.Width = Width;

            my_view.Width = Width;
            my_view.Height = Height - my_addbutton.Height;
            my_view.Top = 0;
            my_view.Left = 0;

            my_addbutton.Left = 0;
            my_addbutton.Top = my_view.Height;
        }

        void my_addbutton_Click(object sender, EventArgs e)
        {
            AddSelectedItem();   
        }

        private void AddSelectedItem()
        {
            //if nothing is selected we do nothing
            if (my_view.SelectedItems.Count == 0)
            {
                return;
            }
            String name = my_view.SelectedItems[0].SubItems[0].Text;
            XmlNode node;
            if (name == "text area")
            {
                node = my_host.Document.CreateTextNode("Click to enter text.");
            }
            else
            {
                node = my_host.Document.CreateElement(name);
            }
            Module adding = my_host.Config.GetInstance(name, node, my_current, my_host);

            my_host.AddAt(my_current, adding);

            //my_current.Children.Add(adding);
            my_host.CalculateSpace();
            my_host.Select(adding);
        }

        public void Use(Module m)
        {
            if (m == null)
            {
                my_current = null;
                my_view.Items.Clear();
                return;
            }
            my_current = m;
            display();
        }

        private void InitializeList()
        {
            my_view.View = View.Details;
            my_view.LabelEdit = true;
            my_view.GridLines = true;

            ColumnHeader prop_name = new ColumnHeader();
            prop_name.Text = "Name";
            prop_name.DisplayIndex = 0;
            prop_name.Width = 146;
            prop_name.Name = "Name";


            my_view.Columns.Clear();
            my_view.Columns.Add(prop_name);
        }

        private void display()
        {
            my_view.Items.Clear();
            TagConfig config = my_current.TagConfig;
            for (int i = 0;  i < config.AllowedChildren.Length; ++i)
            {
                String [] item = new String[1];
                item[0] = config.AllowedChildren[i];
                my_view.Items.Add(new ListViewItem(item));
            }
        }
    }
}
